/*
Copyright (c) 2008 Jeroen Beckers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package restless.aimovement.astar.tiles {
	
	/**
	 * @private
	 * @author Jeroen Beckers (info@dauntless.be)
	 */
	
	import flash.geom.Point;
	import restless.aimovement.astar.Astar;
	import restless.aimovement.astar.interfaces.ICostTile;
	import restless.aimovement.astar.interfaces.IPositionTile;
	
	public class DataTile implements IPositionTile {
		
		private var _target:*;
		private var _g:Number;
		private var _h:Number;
		private var _f:Number;
		
		private var _open:Boolean;
		private var _closed:Boolean;
		private var _parent:DataTile;
		private var _position:Point;
		private var _multiplier:Number = 1;
		private var _standardCost:Number;
		private var tempPoint:Point;
		
		public function DataTile(p_standardCost:Number) {
			_standardCost = p_standardCost;
		}
		
		public function get target():* {
			return _target;
		}
		
		public function set target(target:*):void{
			_target = target;
		}
		
		public function get g():Number {
			return _g;
		}
		
		public function set g(g:Number):void {
			_g = g + cost;
			_f = _h + _g;
		}
		
		public function get h():Number {
			return _h;
		}
		
		public function calculateH(p:Point):void {
			tempPoint = p;
			_h = Math.abs(tempPoint.x - _position.x) * _standardCost + Math.abs(tempPoint.y - _position.y) * _standardCost;
			_f = _h + _g;
			tempPoint = null;
		}
		
		public function set h(num:Number):void {
			_h = num;
		}
		
		public function get f():Number {
			return _f;
		}
		
		public function get open():Boolean {
			return _open;
		}
		
		public function set open(bool:Boolean):void {
			_open = bool;
		}
		
		public function get parent():DataTile {
			return _parent;
		}
		
		public function set parent(parent:DataTile):void {
			_parent = parent;
		}
		
		public function get position():Point {
			return _position;
		}
		
		public function set position(pos:Point):void {
			_position = position;
		}
		
		public function set diag(diag:Boolean):void {
			_multiplier = (diag ? Astar.DIAGONAL_FACTOR : 1);
		}
		
		public function get cost():Number {
			/**
			 * If the target tile has implemented the ICostTile, it has a getCost() that has to be used
			 */
			if (target is ICostTile) {
				return ICostTile(target).cost * _multiplier;
			} else {
				//if not, return standard cost
				return _standardCost * _multiplier;
			}
		}
		
		public function calculateUpdateF(parentCost:Number):Number {
			return (cost + parentCost + h);
		}
	}
}
